<?php

namespace app\index\service;

use app\common\ConfigBase;
use app\index\model\NewskuModel;
use app\index\model\UserModel;
use think\Db;

class LogrecordService
{
    static public function getLog($param){
        $db=Db::table('log_record');
        !empty($param['sku']) && $db->where('sku', '=', $param['sku']);
        !empty($param['operator']) && $db->where('operator', '=', $param['operator']);
        if (!empty($param['creation_time'])) {
            $time = explode(' - ', $param['creation_time']);
            $db->where('creation_time', '>=', $time[0])->where('creation_time', '<=', $time[1]);
        }
        if(empty($param['sku'])&&empty($param['operator'])&&empty($param['creation_time'])){
            $param['_cache']="getLogCount";
        }
        $db->order('id','desc');
        return \app\common\service\ToolService::downOrReturn($db, $param, [],"\app\index\service\LogrecordService::getLogHandle");
    }
    static public function getLogHandle($data){
        //$user=UserModel::getUserById(array_column($data,'operator'));
        $skuName=NewskuModel::getSkuBySku(array_column($data,'sku'));
        $skuStatus=ConfigBase::$skuStatus;
        $skuSaleStatus=ConfigBase::$skuSaleStatus;
        $titles = [
            'sku' => 'SKU',
            'spu' => 'SPU',
            'name' => '采购名称',
            'status' => 'sku状态',
            'sales_status' => '销售状态',
            'remarks' => '备注',
            'develop_code' => '开发编码',
            'cate_name_path' => '分类信息',
            'eancode' => 'EAN码',
            'develop_time' => '开发日期',
            'serviceline' => '业务线',
            'asweight' => '自带包装重量',
            'standard' => '制式',
            'develop_link' => '开发链接',
            'old_erp_id' => '老erp ID',
            'reference_price' => '参考价',
            'qc_standard' => '质检标准',

            'bulk'=>'容量/体积',
            'bulk_unit'=>'单位',
            'battery'=>'是否带电池 0不带 1带',
            'aslength'=>'自带包装长',
            'aswide'=>'自带包装宽',
            'ashight'=>'自带包装高',
            'baleweight'=>'打包后重量(g)',
            'balelength'=>'打包包装长',
            'balewide'=>'打包包装宽',
            'balehight'=>'打包包装高',
            'length'=>'主体尺寸长',
            'wide'=>'主体尺寸宽',
            'hight'=>'主体尺寸高',
            'unit'=>'单位mm',
            'color'=>'多属性 color',
            'type'=>'多属性 type',
            'shape'=>'多属性 shape',
            'size'=>'多属性 size',
            'pattern'=>'多属性 pattern',
            'title_id'=>'标题id',
            'title_val'=>'标题模板值',
            'product_attribute_id'=>'产品属性id',
            'product_attribute_val'=> '产品属性模板值',
            'authorization'=>'是否授权 0未授权 1已授权',
            'logo'=>'品牌logo',
            'packaging_film'=>'包装拍摄 0不拍摄 1拍摄',
            'quantity'=>'数量',
            'quantity_unit'=>'数量单位',
            'research_links'=>'调研链接',
            'developer'=>'开发人员id',
            'category_id'=>'分类id',
            'uptime'=>'上架时间',
        ];
        foreach ($data as &$v){
            $field=[];
            $field2=[];
            $v['old_val']=json_decode($v['old_val'],true);
            $v['new_val']=json_decode($v['new_val'],true);
            if(!empty($v['old_val'])){
                foreach ($v['old_val'] as $k1=>$v2){
                    if($k1=='sales_status'){
                        $v2=$skuSaleStatus[$v2]??$v2;
                    }
                    if($k1=='status'){
                        $v2=$skuStatus[$v2]??$v2;
                    }
                    if(!empty($titles[$k1])){
                        $field[$titles[$k1]]=$v2;
                    }else{
                        $field[$k1]=$v2;
                    }
                }
            }
            if(!empty($v['new_val'])){
                foreach ($v['new_val'] as $k3=>$v3){
                    if($k3=='sales_status'){
                        $v3=$skuSaleStatus[$v3]??$v3;
                    }
                    if($k3=='status'){
                        $v3=$skuStatus[$v3]??$v3;
                    }
                    if(!empty($titles[$k3])){
                        $field2[$titles[$k3]]=$v3;
                    }else{
                        $field2[$k3]=$v3;
                    }
                }
            }
            unset($v['old_val']);
            unset($v['new_val']);
            $v['old']=$field;
            $v['new']=$field2;
            $v['skuName']=$skuName[$v['sku']]??'';
        }
        return $data;
    }
}